Logical operators perform the rough equivalent of a comparison operation on Boolean values. Logical operators use Boolean algebra to evaluate their operands and return the result of the operation. In programming, they are most often used to express complex comparisons which involve multiple operands by linking smaller expressions together.
The unary NOT operator is used preceding a single operand of
BOOLEAN type to inverts the value of the operand. For example, if a variable
z of
BOOLEAN type contains the value
TRUE, then the expression
NOT z will return a value of
FALSE. This operation also holds for the results of more complex expressions; for example, if the result of the expression
p>=q evaluates to
FALSE, the expression
NOT(p>=q) will evaluate to
TRUE.
The AND operator evaluates to
TRUE if and only if the first operand and the second operand both are
TRUE. If either operand evaluates to
FALSE, the result returned will be
FALSE. Expressions using the
AND operator will always evaluate both operands before returning the result of the expression, regardless of the value of the first operand.
The & operator evaluates to
TRUE if and only if the first operand and the second operand are both
TRUE. If either operand evaluates to
FALSE, the result returned will be
FALSE. Expressions using the
& operator will not evaluate the second operand if the first operand returns a value of
FALSE. If the second operand should have any side effects (such as those produced by a function call returning value) they may not occur. In general, it best to avoid expressions such as the following which combine side effects with the
& operator:
The OR operator evaluates to
TRUE if the first operand or the second operand are
TRUE. Both operands must evaluate to
FALSE for the result returned to be
FALSE. Expressions using the
OR operator will always evaluate both operands before returning the result of the expression, regardless of the value of the first operand.
The OR operator evaluates to
TRUE if the first operand or the second operand are
TRUE. Both operands must evaluate to
FALSE for the result returned to be
FALSE. Expressions using the
| operator will not evaluate the second operand if the first operand returns a value of
TRUE. If the second operand should have any side effects (such as those produced by a function call returning value) they may not occur. In general, it is best to avoid expressions such as the following which combine side effects with the
| operator: